package com.googlecode.sc2dm.annotation.processor;

import com.googlecode.sc2dm.annotation.processor.file.FileHandler;
import com.googlecode.sc2dm.annotation.processor.file.FileHandlerImpl;
import com.googlecode.sc2dm.annotation.processor.permissions.Permission;
import com.googlecode.sc2dm.annotation.processor.permissions.UsesPermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Jarle Hansen (jarle@jarlehansen.net)
 * Created: 6:17 PM - 11/20/11
 */
public class ManifestProcessorImpl implements ManifestProcessor {
    private static final Logger logger = LoggerFactory.getLogger(ManifestProcessorImpl.class);

    private final FileHandler manifestHandler;
    private final FileHandler backupManifestHandler;

    private List<String> manifestText;
    private List<String> xmlTags;

    public ManifestProcessorImpl(String manifestdir) {
        manifestHandler = new FileHandlerImpl(new File(manifestdir + "/AndroidManifest.xml"));
        backupManifestHandler = new FileHandlerImpl(new File(manifestdir + "/AndroidManifest-previous.xml"));
    }

    public boolean isValidManifestDir() {
        return manifestHandler.isValidFile();
    }

    public void process() {
        xmlTags = new ArrayList<String>();

        manifestText = manifestHandler.readFile();

        String manifestTextString = createManifestTextString();
        logger.info(manifestTextString);

        populatePermissions(manifestTextString);
        populateUsesPermissions(manifestTextString);
        writeXmlTags();
    }

    private String createManifestTextString() {
        StringBuilder sb = new StringBuilder();

        for (String line : manifestText) {
            sb.append(line).append("\n");
        }

        return sb.toString();
    }

    private void populatePermissions(String manifestTextString) {
        Permission[] permissions = Permission.values();
        for (Permission permission : permissions) {
            if (!manifestTextString.contains(permission.value()))
                xmlTags.add(permission.getXmlTag());
        }
    }

    private void populateUsesPermissions(String manifestTextString) {
        UsesPermission[] usesPermissions = UsesPermission.values();

        for (UsesPermission usesPermission : usesPermissions) {
            if (!manifestTextString.contains(usesPermission.value()))
                xmlTags.add(usesPermission.getXmlTag());
        }
    }

    private void writeXmlTags() {
        if (xmlTags.size() > 0) {
            logger.info("Adding xml tags: {}", xmlTags);
            backupManifestHandler.writeFile(manifestText);

            manifestText.add(manifestText.size() - 1, "\n\t<!-- Generated by Simple-C2DM, " + new Date().toString() + " -->");
            manifestText.addAll(manifestText.size() - 1, xmlTags);
            manifestHandler.writeFile(manifestText);
        } else {
            logger.info("No changes needed");
        }
    }
}
